package com.example.zyfypt_321lz.model;

import com.example.zyfypt_321lz.bean.VideoBean1;
import com.example.zyfypt_321lz.iface.VideoListener1;
import com.example.zyfypt_321lz.iface.Videoiface1;
import com.example.zyfypt_321lz.service.VideoService1;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by HP on 2017/5/31.
 */

public class VideoModel1 implements Videoiface1 {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造函数
    public VideoModel1(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取文章数据
    @Override
    public void getResultList(String video, int page, String sessionID, final VideoListener1 videoListener1) {
        //使用Retrofit-2
        VideoService1 videoService1=retrofit.create(VideoService1.class);
        Call<List<VideoBean1>> call = videoService1.getResult(video,page,sessionID);
        //使用Retrofit-3
        call.enqueue(new Callback<List<VideoBean1>>() {
            @Override
            public void onResponse(Call<List<VideoBean1>> call, Response<List<VideoBean1>> response) {
                if(response.isSuccessful()&&response!=null){
                    videoListener1.onResponse(response.body());
                }
                else
                    videoListener1.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<VideoBean1>> call, Throwable t) {
                videoListener1.onFail(t.toString());
            }
        });
    }
}